package com.bytenebula.asteroid.service.impl;

import com.bytenebula.entity.meta.MetaColumn;
import com.bytenebula.entity.meta.MetaTable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定义MySQL数据库抽取器
 *
 * @author patrick
 * @since 2024-09-23
 */
@Component
public class MysqlExtractService extends ExtractServiceImpl {

    /**
     * 根据数据库名称抽取数据库里所有的表信息
     *
     * @param db_name 待抽取数据库名称
     * @return 返回数据表列表
     */
    @Override
    public List<MetaTable> extractTables(String db_name) {
        String sql_tpl = "SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM information_schema.tables WHERE TABLE_SCHEMA='%s'";
        return super.fetchTables(db_name, String.format(sql_tpl, db_name));
    }

    /**
     * 根据数据表名称抽取列信息
     *
     * @param table_name 待抽取数据表名称
     * @return 返回数据列列表
     */
    @Override
    public List<MetaColumn> extractColumns(String db_name, String table_name) {
        String sql_tpl = "SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s' ORDER BY ORDINAL_POSITION";
        return super.fetchColumns(db_name, String.format(sql_tpl, db_name, table_name));
    }
}
